﻿using B.S.DataBase.MyProject.Domain.Management;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.Management;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.Management
{
    /// <summary>
    /// 处理修改状态命令
    /// </summary>
    public class UpdateReportsCommandHandler : IRequestHandler<UpdateReportsCommand, APIResult<object>>
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<UpdateReportsCommandHandler> logger;
        /// <summary>
        /// sqlSugar
        /// </summary>
        private readonly SqlSugarContext sqlSugar;
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="sqlSugar">sqlSugar</param>
        public UpdateReportsCommandHandler(ILogger<UpdateReportsCommandHandler> logger,SqlSugarContext sqlSugar)
        {
            this.logger = logger;
            this.sqlSugar = sqlSugar;
        }

        public async Task<APIResult<object>> Handle(UpdateReportsCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<object> result = new APIResult<object>();
                var entity = sqlSugar.db.Queryable<Reports>().First(x => x.ReportsId == request.ReportsId);
                entity.Status=request.Status.ToString();
                entity.Publisher = 1;
                var i = sqlSugar.db.Updateable(entity).ExecuteCommand();
                if (i>0)
                {
                    result.Code = APIEnum.修改成功;
                    result.Msg = APIEnum.修改成功.ToString();
                    result.data= i;
                }
                else
                {
                    result.Code = APIEnum.修改失败;
                    result.Msg = APIEnum.修改失败.ToString();
                    result.data = i;
                }
                return await Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
